﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using AMS.Core.DataAccess;
using AMS.Core.Entities;
using AMS.Core.Helper;
using AMS.Core.Importors;
using AMS.History.DataAccess;
using AMS.History.Entities;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace AMS.History.Importor
{
    public class AssetInfoImportor : AbstractImportor
    {
        private const string BARCODE = "资产条形码";
        private const string PARENTBARCODE = "父资产条形码";
        private const string ASSETTYPECODE = "类别编码";
        private const string ASSETTYPENAME = "类别名称";
        private const string CODE = "资产编号";
        private const string NAME = "资产名称";
        private const string MODELNUMBER = "规格型号";
        private const string SIZEINFO = "外形尺寸";
        private const string UNIT = "单位";
        private const string SITE = "地点";
        private const string LOCATION = "详细位置";
        private const string ORIGINALVALUE = "资产原值";
        private const string ENDVALUE = "资产净值";
        private const string NATURE = "资产性质";
        private const string SOURCE = "资产来源";
        private const string DEPRECIATIONFUNCTION = "折旧方法";
        private const string DEPRECIATIONYEARS = "折旧年限";
        private const string DEPRECIATIONRATE = "年折旧率";
        private const string DEPRECIATIONVALUE = "累计折旧额";
        private const string SUPPLIER = "供应商";
        private const string SERIALNUMBER = "出厂编号";
        private const string MANUFACTURERS = "制造商";
        private const string SHELFLIFE = "质保期";
        private const string USEYEARS = "使用年限";
        private const string STATE = "资产状态";
        private const string DEPARTMENT = "使用部门";
        private const string USERS = "使用人";
        private const string CHECKEDDATE = "验收日期";
        private const string STARTUPDATE = "启用日期";

        private const string MEMO = "备注";
        private const string KEY_WORD = "关键字";
        private const string OPERATE = "操作";

        private const string m_ObjectKeyProName = "Barcode";
        private const string ObjectName = "资产";
        private CoreDataAccess m_Access;
        private string m_ImportType = "";

        private User m_CurrUser;
        private NameObjectDictionary m_nodParentBarcodes = new NameObjectDictionary();
        private StringCollection m_scParentBarcodes = new StringCollection();

        private NameObjectDictionary m_nodUnits = new NameObjectDictionary();
        private StringCollection m_scUnits = new StringCollection();

        private NameObjectDictionary m_nodSites = new NameObjectDictionary();
        private StringCollection m_scSites = new StringCollection();

        private NameObjectDictionary m_nodNatures = new NameObjectDictionary();
        private StringCollection m_scNatures = new StringCollection();

        private NameObjectDictionary m_nodSources = new NameObjectDictionary();
        private StringCollection m_scSources = new StringCollection();

        private NameObjectDictionary m_nodStates = new NameObjectDictionary();
        private StringCollection m_scStates = new StringCollection();

        private NameObjectDictionary m_nodDepartments = new NameObjectDictionary();
        private StringCollection m_scDepartments = new StringCollection();

        private NameObjectDictionary m_nodUsers = new NameObjectDictionary();
        private StringCollection m_scUsers = new StringCollection();

        private NameObjectDictionary m_nodAssetTypeInfos = new NameObjectDictionary();
        private StringCollection m_scAssetTypeKeyWords = new StringCollection();

        private NameObjectDictionary m_nodObjectByBarcodes = new NameObjectDictionary();
        private NameValueCollection m_ProNameSet;

        private BarcodeFormat m_BarcodeFormat;


        private StringCollection m_scBarcodes = new StringCollection();

        public AssetInfoImportor(string path, string sheetName, User currentUserCode, string importType)
        {
            m_ErrorMessage = new ErrorResult { TableName = "出错信息" };
            FilePath = path;
            SheetName = sheetName;
            m_ImportType = importType;
            m_IsTrans = true;
            m_CurrUser = currentUserCode;
            m_ImportNum = DataAccess.GetImportNum();
            var obj = DataAccess.GetSingleObjectByProVal(typeof (BarcodeFormat).Name, "AssetTypeName", importType);
            if (obj != null && obj is BarcodeFormat)
                m_BarcodeFormat = ((BarcodeFormat) obj);
        }

        private NameValueCollection ProNameSet
        {
            get
            {
                if (m_ProNameSet == null) //简单属性绑定,用于DataRowToObject
                {
                    m_ProNameSet = new NameValueCollection
                                       {
                                           {NAME, "Name"},
                                           {BARCODE, "Barcode"},
                                           {MODELNUMBER, "ModelNumber"},
                                           {SIZEINFO, "SizeInfo"},
                                           {LOCATION, "Location"},
                                           {ORIGINALVALUE, "OriginalValue"},
                                           {ENDVALUE,"EndValue"},
                                           {DEPRECIATIONFUNCTION, "DepreciationFunction"},
                                           {DEPRECIATIONYEARS, "DepreciationYears"},
                                           {DEPRECIATIONRATE, "DepreciationRate"},
                                           {DEPRECIATIONVALUE, "DepreciationValue"},
                                           {SUPPLIER, "Supplier"},
                                           {SERIALNUMBER, "SerialNumber"},
                                           {MANUFACTURERS, "Manufacturers"},
                                           {SHELFLIFE, "ShelfLife"},
                                           {USEYEARS, "UseYears"},
                                           {CHECKEDDATE, "CheckedDate"},
                                           {STARTUPDATE, "StartUpDate"},
                                           {KEY_WORD, "KeyWord"},
                                           {MEMO, "Memo"}
                                       };
                }
                return m_ProNameSet;
            }
        }

        private CoreDataAccess DataAccess
        {
            get
            {
                if (m_Access == null)
                    m_Access = new CoreDataAccess();
                return m_Access;
            }
        }

        protected override string[] Titles
        {
            get
            {
                if (m_ImportType == "SubAsset")
                {
                    return new[] { BARCODE, PARENTBARCODE, ASSETTYPECODE,ASSETTYPENAME,NAME,MODELNUMBER,SIZEINFO,UNIT,
                    SITE,LOCATION,ORIGINALVALUE,ENDVALUE,NATURE,SOURCE,DEPRECIATIONFUNCTION,DEPRECIATIONYEARS,DEPRECIATIONRATE,
                    DEPRECIATIONVALUE,SUPPLIER,SERIALNUMBER,MANUFACTURERS,SHELFLIFE,USEYEARS,STATE,DEPARTMENT,USERS,
                    CHECKEDDATE,STARTUPDATE,MEMO, KEY_WORD, OPERATE };
                }

                return new[] { BARCODE,  ASSETTYPECODE,ASSETTYPENAME,NAME,MODELNUMBER,SIZEINFO,UNIT,
                SITE,LOCATION,ORIGINALVALUE,ENDVALUE,NATURE,SOURCE,DEPRECIATIONFUNCTION,DEPRECIATIONYEARS,DEPRECIATIONRATE,
                DEPRECIATIONVALUE,SUPPLIER,SERIALNUMBER,MANUFACTURERS,SHELFLIFE,USEYEARS,STATE,DEPARTMENT,USERS,
                CHECKEDDATE,STARTUPDATE,MEMO, KEY_WORD, OPERATE };


            }
        }

        protected override object DataRowToObject(DataRow row)
        {
            var strKeyVal = Convert.ToString(ReturnUpperColumnValue(row, BARCODE)).Trim();
            var isInsert = (ReturnUpperColumnValue(row, OPERATE) == INSERT);

            AssetInfo result;
            if (isInsert)
            {
                result = new AssetInfo { Barcode = strKeyVal, ImportNum = m_ImportNum };
                InsertCount++;
            }
            else
            {
                result = (AssetInfo)m_nodObjectByBarcodes[strKeyVal];
                UpdateCount++;
            }

            for (int i = 0; i < ProNameSet.Count; i++)
            {
                var strProName = ProNameSet.Get(i);
                var strColName = ProNameSet.GetKey(i);
                if (!IsNeedToUpdateInfo(isInsert, row, strColName)) continue;
                var strColValue = strProName == "Barcode" ? ReturnUpperColumnValue(row, strColName) : ReturnColumnValue(row, strColName);

                if (strProName == "DepreciationRate")
                {

                    var value = strColValue.Substring(0, strColValue.Length - 1);

                    strColValue = (Convert.ToDouble(value) / 100).ToString();
                }
                else if(strProName == "DepreciationFunction")
                {
                    switch (strColValue)
                    {
                        case "平均年限折旧法":
                            strColValue = "AverageDepreciation";
                            break;
                        case "加速折旧法":
                            strColValue = "SpeedDepreciation";
                            break;
                        case "工作量折旧法":
                            strColValue = "WorkloadDepreciation";
                            break;
                        default:
                            strColValue = "";
                            break;
                    }
                }

                PropertyInfo pi = result.GetType().GetProperty(strProName);
                if (pi == null) continue;
                if (pi.PropertyType == typeof(String) || pi.PropertyType == typeof(Int16)
                    || pi.PropertyType == typeof(Int32) || pi.PropertyType == typeof(Int64))
                    pi.SetValue(result, strColValue, null);
                else if (pi.PropertyType == typeof(Double))
                {
                    double OriginalValue = Convert.ToDouble(strColValue);
                    pi.SetValue(result, OriginalValue, null);
                }
                else if (pi.PropertyType == typeof(DateTime))
                {
                    if (strColValue.Trim().Length > 0)
                    {
                        DateTime dateValue = Convert.ToDateTime(strColValue);
                        pi.SetValue(result, dateValue, null);
                    }

                }
               
            }
            IList barcodeFormatList = m_Access.GetObjectList(new BarcodeFormat().GetType().Name, "AssetTypeName",
                                                                             m_ImportType);
            if (barcodeFormatList != null && barcodeFormatList.Count > 0)
            {
                var barcodeFormat = (BarcodeFormat)barcodeFormatList[0];
                result.BarcodeFormat = barcodeFormat;
            }

            switch (m_ImportType)
            {
                case "MainAsset":
                    result.TypeName = "MainAsset";
                    break;
                case "LowValueAsset":
                    result.TypeName = "LowValueAsset";
                    break;
                case "StorageAsset":
                    result.TypeName = "StorageAsset";
                    break;
                default:
                    result.TypeName = "SubAsset";
                  
                    if (IsNeedToUpdateInfo(isInsert, row, PARENTBARCODE))
                    {
                        var strParentBarcodeValue = ReturnUpperColumnValue(row, PARENTBARCODE);
                        if (m_nodParentBarcodes.Contains(strParentBarcodeValue))
                            result.ParentBarcode = (m_nodParentBarcodes[strParentBarcodeValue].ToString());
                    }
                    break;
            }


            if (IsNeedToUpdateInfo(isInsert, row, UNIT))
            {
                var strUnitValue = ReturnColumnValue(row, UNIT);
                if (m_nodUnits.Contains(strUnitValue))
                    result.Unit = ((AssetUnit)m_nodUnits[strUnitValue]);
            }
            if (IsNeedToUpdateInfo(isInsert, row, SITE))
            {
                var strSiteValue = ReturnColumnValue(row, SITE);
                if (m_nodSites.Contains(strSiteValue))
                    result.Site = ((Site)m_nodSites[strSiteValue]);
            }
            if (IsNeedToUpdateInfo(isInsert, row, NATURE))
            {
                var strNatureValue = ReturnColumnValue(row, NATURE);
                if (m_nodNatures.Contains(strNatureValue))
                    result.Nature = ((AssetNature)m_nodNatures[strNatureValue]);
            }
            if (IsNeedToUpdateInfo(isInsert, row, SOURCE))
            {
                var strSourceValue = ReturnColumnValue(row, SOURCE);
                if (m_nodSources.Contains(strSourceValue))
                    result.Source = ((AssetSource)m_nodSources[strSourceValue]);
            }
            if (IsNeedToUpdateInfo(isInsert, row, STATE))
            {
                var strStateValue = ReturnColumnValue(row, STATE);
                if (m_nodStates.Contains(strStateValue))
                    result.State = ((AssetState)m_nodStates[strStateValue]);
            }
            if (IsNeedToUpdateInfo(isInsert, row, DEPARTMENT))
            {
                var strUserGroupValue = ReturnColumnValue(row, DEPARTMENT);
                if (m_nodDepartments.Contains(strUserGroupValue))
                    result.Department = ((UserGroup)m_nodDepartments[strUserGroupValue]);
            }
            if (IsNeedToUpdateInfo(isInsert, row, USERS))
            {
                var strUserValue = ReturnColumnValue(row, USERS);
                if (m_nodUsers.Contains(strUserValue))
                    result.Users = ((User)m_nodUsers[strUserValue]);
            }
            if (IsNeedToUpdateInfo(isInsert, row, ASSETTYPECODE) && IsNeedToUpdateInfo(isInsert, row, ASSETTYPENAME))
            {
                var strAssetTypeKeyVal= ReturnColumnValue(row, ASSETTYPECODE) +BaseDataAccess.KEY_VAL_FG+ ReturnColumnValue(row, ASSETTYPENAME);

                if (m_nodAssetTypeInfos.Contains(strAssetTypeKeyVal))
                    result.AssetType = ((AssetType)m_nodAssetTypeInfos[strAssetTypeKeyVal]);
            }
            result.Code = ReturnColumnValue(row, ASSETTYPECODE);
            return result;
        }

        protected override void SaveObjects(IList objects)
        {
            var hisAccess = new HistoryAccess { Session = DataAccess.Session };
            var moObjs = new MonitorObjects();
            var hisVersion = new HisVersionInfo { Memo = "批量更新", OperatorUserId = GenericContext.CurrUserId };
            if (IsTrans)
                DataAccess.BeginTransaction();
            try
            {
                foreach (var obj in objects)
                {
                    DataAccess.SaveOrUpdate(obj);
                    moObjs.Listen(obj);
                }
                hisAccess.SaveMonitorObjs(moObjs, typeof(AssetInfo), typeof(AssetInfoRecord), "Id", hisVersion);
                if (IsTrans)
                    DataAccess.Commit();
            }
            catch (Exception)
            {
                if (m_IsTrans)
                    DataAccess.RollBack();
                throw;
            }
            finally
            {
                if (m_IsTrans)
                    DataAccess.CloseSession();
            }
        }

        protected override void GetDBRecordSet()
        {
            try
            {
                m_nodObjectByBarcodes = DataAccess.GetObjectsByKeyValues("AssetInfo", m_ObjectKeyProName, m_scBarcodes);
                m_nodParentBarcodes = DataAccess.GetObjectsByKeyValues("AssetInfo", "Barcode", m_scParentBarcodes);
                m_nodUnits = DataAccess.GetObjectsByKeyValues("AssetUnit", "Name", m_scUnits);
                m_nodSites = DataAccess.GetObjectsByKeyValues("Site", "Name", m_scSites);
                m_nodNatures = DataAccess.GetObjectsByKeyValues("AssetNature", "Name", m_scNatures);
                m_nodSources = DataAccess.GetObjectsByKeyValues("AssetSource", "Name", m_scSources);
                m_nodStates = DataAccess.GetObjectsByKeyValues("AssetState", "Name", m_scStates);
                m_nodDepartments = DataAccess.GetObjectsByKeyValues("UserGroup", "Name", m_scDepartments);
                m_nodUsers = DataAccess.GetObjectsByKeyValues("User", "Name", m_scUsers);
                m_nodAssetTypeInfos = DataAccess.GetAssetTypeInfos(m_scAssetTypeKeyWords);
                
            }
            finally
            {
                DataAccess.CloseSession();
            }
        }

        protected override ExcelHelper CreateExcelHelper(string path, string sheetName)
        {
            var helper = new ExcelHelper(path, sheetName);
            helper.NotRepleateColumns.AddColumnByName(BARCODE);
            helper.NotEmptyColumns.AddColumnByName(OPERATE);
            return helper;
        }

        

        protected override void CreateDBSearchCondition(DataRow row, ErrorResult errorResult, int excelRowNumber)
        {
            string Message;
            var strObjKeyVal = ReturnUpperColumnValue(row, BARCODE);


            var operate = Convert.ToString(row[OPERATE]).Trim();

            bool isInsert = (operate == INSERT);

            //控制非空字段的导入与更新操作
            var insertNoEmptyTitles =
                new[]
                    {
                        BARCODE,NAME,ASSETTYPECODE,ASSETTYPENAME,UNIT,SITE,ORIGINALVALUE,ENDVALUE,DEPRECIATIONFUNCTION,
                        DEPRECIATIONYEARS,DEPRECIATIONRATE, USEYEARS,STATE,DEPARTMENT,
              
                    };

            CheckInsertNoEmptyColumn(row, insertNoEmptyTitles, isInsert, errorResult, excelRowNumber);

            //不允许清除内容的列
            var updateNoDeleteTitles =
                new[]
                   {
                         BARCODE,NAME,ASSETTYPECODE,ASSETTYPENAME,UNIT,SITE,ORIGINALVALUE,ENDVALUE, DEPRECIATIONFUNCTION, DEPRECIATIONYEARS,DEPRECIATIONRATE, USEYEARS,STATE,DEPARTMENT,

                   };

            CheckUpdateNoDeleteField(row, updateNoDeleteTitles, isInsert, errorResult, excelRowNumber);


            if (!CheckFieldLength(row, BARCODE, 6, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strObjKeyVal) && strObjKeyVal.Length > 0 &&
               !m_scBarcodes.Contains(strObjKeyVal))
                m_scBarcodes.Add(strObjKeyVal);
            if(m_BarcodeFormat !=null && m_BarcodeFormat.Prefix.Trim().Length > 0)
            {
                if (!CheckBarCodeFormat(row, BARCODE, m_BarcodeFormat.Prefix, out Message))
                    errorResult.SetMessage(excelRowNumber, Message);
            }
          

            if (!CheckFieldLength(row, MODELNUMBER, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, SIZEINFO, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, LOCATION, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, ORIGINALVALUE, 1, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckIsPlusDouble(row[ORIGINALVALUE].ToString(), ORIGINALVALUE, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, ENDVALUE, 1, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckIsPlusDouble(row[ENDVALUE].ToString(), ENDVALUE, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (row[ORIGINALVALUE].ToString().Trim().Length > 0 && row[ENDVALUE].ToString().Trim().Length > 0)
            {
                if (CheckIsPlusDouble(row[ORIGINALVALUE].ToString(), ORIGINALVALUE, out Message) && CheckIsPlusDouble(row[ENDVALUE].ToString(), ENDVALUE, out Message))
                {
                    if (Convert.ToDouble(row[ENDVALUE].ToString()) > Convert.ToDouble(row[ORIGINALVALUE].ToString()))
                        errorResult.SetMessage(excelRowNumber, "资产净值不能大于资产原值!");
                }
              
            }
           if (!CheckFieldLength(row, DEPRECIATIONFUNCTION, 1, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, DEPRECIATIONYEARS, 1, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, DEPRECIATIONRATE, 1, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckPercent(row[DEPRECIATIONRATE].ToString(), DEPRECIATIONRATE, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, USEYEARS, 1, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckIsPlusInt(row[USEYEARS].ToString(), USEYEARS, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (row[CHECKEDDATE].ToString().Trim().Length > 0)
                if (!CheckYearMonthDayLeapStringFormat(row[CHECKEDDATE].ToString(), CHECKEDDATE, out Message))
                    errorResult.SetMessage(excelRowNumber, Message);
            if (row[STARTUPDATE].ToString().Trim().Length > 0)
                if (!CheckYearMonthDayLeapStringFormat(row[STARTUPDATE].ToString(), STARTUPDATE, out Message))
                    errorResult.SetMessage(excelRowNumber, Message);

            if (m_ImportType == "SubAsset")
            {
                if (!CheckFieldLength(row, PARENTBARCODE, 1, 50, out Message))
                    errorResult.SetMessage(excelRowNumber, Message);
                var strParentBarcodeName = ReturnColumnValue(row, PARENTBARCODE);
                if (IsNeedToUpdateInfo(isInsert, strParentBarcodeName) && strParentBarcodeName.Length > 0 &&
               !m_scParentBarcodes.Contains(strParentBarcodeName))
                    m_scParentBarcodes.Add(strParentBarcodeName);
            }
            var strSiteName = ReturnColumnValue(row, SITE);
            if (!CheckFieldLength(row, SITE, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strSiteName) && strSiteName.Length > 0 &&
                !m_scSites.Contains(strSiteName))
                m_scSites.Add(strSiteName);

            var strDepartmentName = ReturnColumnValue(row, DEPARTMENT);
            if (!CheckFieldLength(row, DEPARTMENT, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strDepartmentName) && strDepartmentName.Length > 0 &&
                !m_scDepartments.Contains(strDepartmentName))
                m_scDepartments.Add(strDepartmentName);

            var strUserName = ReturnColumnValue(row, USERS);
            if (!CheckFieldLength(row, USERS, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strUserName) && strUserName.Length > 0 &&
                !m_scUsers.Contains(strUserName))
                m_scUsers.Add(strUserName);

            var strNatureName = ReturnColumnValue(row, NATURE);
            if (!CheckFieldLength(row, NATURE, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strNatureName) && strNatureName.Length > 0 &&
                !m_scNatures.Contains(strNatureName))
                m_scNatures.Add(strNatureName);

            var strSourceName = ReturnColumnValue(row, SOURCE);
            if (!CheckFieldLength(row, SOURCE, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strSourceName) && strSourceName.Length > 0 &&
                !m_scSources.Contains(strSourceName))
                m_scSources.Add(strSourceName);

            var strUnitName = ReturnColumnValue(row, UNIT);
            if (!CheckFieldLength(row, UNIT, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strUnitName) && strUnitName.Length > 0 &&
                !m_scUnits.Contains(strUnitName))
                m_scUnits.Add(strUnitName);

            var strStateName = ReturnColumnValue(row, STATE);
            if (!CheckFieldLength(row, STATE, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (IsNeedToUpdateInfo(isInsert, strStateName) && strStateName.Length > 0 &&
                !m_scStates.Contains(strStateName))
                m_scStates.Add(strStateName);

            string strTypeCode = Convert.ToString(row[ASSETTYPECODE]);
            string strTypeName = Convert.ToString(row[ASSETTYPENAME]);


            string strTypeKeyVal = strTypeCode + BaseDataAccess.KEY_VAL_FG + strTypeName;

            if (!CheckFieldLength(row, ASSETTYPECODE, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);
            if (!CheckFieldLength(row, ASSETTYPENAME, 0, 50, out Message))
                errorResult.SetMessage(excelRowNumber, Message);

            if (IsNeedToUpdateInfo(isInsert, strTypeCode) && strTypeCode.Length > 0 && IsNeedToUpdateInfo(isInsert, strTypeName) && strTypeName.Length > 0&&
                !m_scAssetTypeKeyWords.Contains(strTypeKeyVal))
                m_scAssetTypeKeyWords.Add(strTypeKeyVal);
        }



        private bool CheckBarCodeFormat(DataRow row, string fieldname, string prefix, out string Message)
        {
            string field = Convert.ToString(row[fieldname]).Trim();
            return CheckBarCodeFormatExist(field, fieldname, prefix, out Message);

        }

        private bool CheckBarCodeFormatExist(string field, string fieldname, string prefix, out string Message)
        {
            Message = null;

            if (field.Contains(prefix))
            {
                var startIndex = prefix.Length;
                var endIndex = field.Length - prefix.Length;
                if(endIndex < 4) //最少要4位流水号
                {
                    Message = fieldname + ":" + field + "不符合系统条码生成规则，前缀应该为:" + prefix + "流水号不能少于4位";
                    return false;
                }
                if (!CheckIsPlusInt(field.Substring(startIndex, endIndex), "", out  Message))
                {
                    Message = fieldname + ":" + field + "不符合系统条码生成规则，前缀应该为:" + prefix + ",并且条码长度不能少于6位";
                    return false;
                }
                return true;
            }
            Message = fieldname + ":" + field + "不符合系统条码生成规则，前缀应该为:" + prefix + ",并且条码长度不能少于6位";
            return false;

        }

        protected override void CheckInDb(DataRow row, int excelRowNumber, ErrorResult errorResult)
        {
            var isInsert = (ReturnColumnValue(row, OPERATE) == INSERT);
            string msg;
            var strObjKeyProVal = ReturnColumnValue(row, BARCODE);

            if (isInsert)
            {
                if (CheckChildObjectExists(m_nodObjectByBarcodes, strObjKeyProVal, m_ObjectKeyProName, out msg))
                    errorResult.SetMessage(excelRowNumber,
                                           m_ObjectKeyProName + "为: [ " + strObjKeyProVal + " ] 的" + ObjectName +
                                           "已经存在不能导入");
            }
            else
            {
                if (!CheckChildObjectExists(m_nodObjectByBarcodes, strObjKeyProVal, m_ObjectKeyProName, out msg))
                    errorResult.SetMessage(excelRowNumber,
                                           m_ObjectKeyProName + "为: [ " + strObjKeyProVal + " ] 的" + ObjectName +
                                           "不存在不能更新");
            }

            if (m_ImportType == "SubAsset")
            {
                if (CheckFieldLengthNotZero(row[PARENTBARCODE].ToString(), PARENTBARCODE))
                {
                    if (!CheckChildObjectExists(m_nodParentBarcodes, row, PARENTBARCODE, out msg))
                        errorResult.SetMessage(excelRowNumber, msg);
                }
            }
            if (!CheckChildObjectExists(m_nodUnits, row, UNIT, out msg))
                errorResult.SetMessage(excelRowNumber, msg);
            if (!CheckChildObjectExists(m_nodStates, row, STATE, out msg))
                errorResult.SetMessage(excelRowNumber, msg);
            if (CheckFieldLengthNotZero(row[NATURE].ToString(), NATURE))
            {
                if (!CheckChildObjectExists(m_nodNatures, row, NATURE, out msg))
                    errorResult.SetMessage(excelRowNumber, msg);
            }
            if (CheckFieldLengthNotZero(row[SOURCE].ToString(), SOURCE))
            {
                if (!CheckChildObjectExists(m_nodSources, row, SOURCE, out msg))
                    errorResult.SetMessage(excelRowNumber, msg);
            }

            if (CheckFieldLengthNotZero(row[USERS].ToString(), USERS))
            {
                if (!CheckChildObjectExists(m_nodUsers, row, USERS, out msg))
                    errorResult.SetMessage(excelRowNumber, msg);
            }

            if (!CheckChildObjectExists(m_nodDepartments, row, DEPARTMENT, out msg))
                errorResult.SetMessage(excelRowNumber, msg);
            if (!CheckChildObjectExists(m_nodSites, row, SITE, out msg))
                errorResult.SetMessage(excelRowNumber, msg);

        }
    }
}
